<div class="container-fluid">
    <div class="row">
        <div class="col-md-12 mb-3">
            <div class="input-group">
                <button class="btn btn-warning" id="btnToImage">转成图片</button>
                <div class="custom-file">
                    <input id="txtFile" type="file" class="custom-file-input">
                    <label class="custom-file-label" id="pr">选择（拖拽、粘贴）图片或文本</label>
                </div>
                <div class="input-group-prepend">
                    <button class="btn btn-danger" id="btnToText">转回文本</button>
                </div>
                <input class="form-control" id="txtUri" placeholder="粘贴图片链接转为文本（须支持跨域）" />
                <div class="input-group-prepend">
                    <button class="btn btn-info" id="btnToTextDemo">例子</button>
                </div>
            </div>
        </div>
        <div class="col-md-12 mb-3">
            <div id="editor" class="border overflow-hidden position-relative min300">
            </div>
        </div>
        <div class="col-md-12 mb-3 nrView d-none">
            <img class="mw-100 border" style="height:200px" />
            <br />
            <a href="javascript:void(0)" target="_blank" download="tti.png">下载</a>
        </div>
        <div class="col-md-12 mb-3">
            <p>把文字转换为图片；转换后的图片可转回文字（图片未压缩等丢失信息的操作）</p>
            <p>可以把文字转为图片后上传到图床（图床选不处理图片保存原图的厂商，且支持跨域）用于存储</p>
            <p class="text-danger">警告：非文字转成的图片解析后是乱码，而且耗时很久，容易卡死浏览器</p>
        </div>
    </div>
</div>

@Html.Raw(Netnr.SharedApp.QuoteTo.Html("monaco-editor,tti.js"))

<script>

    var ti = {
        tti: new tti(),
        editor: null,
        init: function () {

            //接收文件
            ss.receiveFiles(function (files) {
                var file = files[0]
                console.log(files, file);
                //解析
                if (file.type.indexOf("image") == 0) {
                    ti.showText(URL.createObjectURL(file));
                } else {
                    var reader = new FileReader();
                    reader.onload = function () {
                        if (reader.result) {
                            ti.editor.setValue(reader.result);
                        }
                    };
                    reader.readAsText(file);
                }
            }, "#txtFile");

            $('#btnToImage').click(function () {
                var txt = ti.editor.getValue();
                if (txt != "") {
                    ti.showImage(txt);
                }
            });

            $('#btnToText').click(function () {
                var uri = $('#txtUri').val();
                if (uri != "") {
                    ti.showText(uri);
                }
            });

            $('#btnToTextDemo').click(function () {
                var uri = 'https://img14.360buyimg.com/ddimg/jfs/t1/125681/26/19217/154989/5fb66c5fE20b7c048/fa5a3f63b1179289.png';
                $('#txtUri').val(uri);
                $('#btnToText')[0].click();
            });

            ti.showCode('');
        },
        showCode: function (code) {
            if (ti.editor) {
                ti.editor.setValue(code);
            } else {
                require(['vs/editor/editor.main'], function () {
                    ti.editor = monaco.editor.create(document.getElementById("editor"), {
                        value: code,
                        language: 'html',
                        automaticLayout: true,
                        wordWrap: "on",
                        theme: 'vs',
                        scrollbar: {
                            verticalScrollbarSize: 6,
                            horizontalScrollbarSize: 6
                        },
                        minimap: {
                            enabled: false
                        }
                    });
                });
            }
        },
        showText: function (src) {
            var nv = $('.nrView').removeClass('d-none').children();

            ti.showCode("正在解析为文本，请稍等 ... \r\n非文字转成的图片解析后是乱码 ... \r\n图片越大（亮）解析越慢 ... ");
            nv.first().attr('src', src);
            nv.last().attr('href', src);
            setTimeout(function () {
                try {
                    ti.tti.asText(nv.first()[0], function (txt) {
                        ti.showCode(txt)
                    });
                } catch (e) {
                    console.log(e);
                    ti.showCode(e + "");
                }
            }, 1000)
        },
        showImage: function (txt) {
            var $base64 = ti.tti.asImage(txt).toImage();
            var nv = $('.nrView').removeClass('d-none').children();
            nv.first().attr('src', $base64);
            nv.last().attr('href', nv.first().attr('src'));
        }
    }

    ti.init();
</script>